package com.daocao.hua.adapter.base;

import android.content.Context;
import android.support.annotation.AnimRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.daocao.hua.interfaces.OnRecyclerViewListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Created by on 17/3/30
 * RecyclerView一般使用步骤：
 * 1.继承RecyclerView.Adapter<HomeAdapter.MyViewHolder>，并传入自己实现的MyViewHolder
 *   class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>
 *      //构造方法中传入数据
 *      //重写 onBindViewHolder方法中为控件绑定数据
 *       //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
 *       View view = inflater.inflate(R.layout. item_home,parent, false);
 MyViewHolder holder= new MyViewHolder(view);
 return holder;
 并定义一些对数据的操作，单击事件等
 *抽象封装RecyclerView适配器基类：
 *  抽取相同的部分封装，不同的部分设为抽象方法，交给子类（数据绑定），数据传入用泛型，ViewHolder中的成员变量交由view数组处理
 * 1.BaseRecyclerAdapter<T, V extends BaseViewHolder>
 * 2.构造器传参：BaseRViewAdapter(Context context, int layoutResId, List<T> datas)
 *3.onBindViewHolder中绑定数据，先getItem获取数据，再传入bindData(viewholder,T),
 * 4.onCreateViewHolder中返回一个视图，由inflate传入子view，和点击事件
 */
public abstract class BaseRViewAdapter<T,H extends  BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder>{



    protected static final String TAG = BaseRViewAdapter.class.getSimpleName();

    protected final Context context;//可以有外界传入，或者parent自己获取

    protected  int layoutResId;

    protected List<T> datas;


    //private OnItemClickListener mOnItemClickListener = null;
    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }


    public  interface OnItemClickListener {
        void onItemClick(View view, int position);
    }



    public BaseRViewAdapter(Context context, int layoutResId) {
        this(context, layoutResId, null);
    }


    public BaseRViewAdapter(Context context, int layoutResId, List<T> datas) {
        this.datas = datas == null ? new ArrayList<T>() : datas;
        this.context = context;
        this.layoutResId = layoutResId;
    }



    /**
     * 创建视图，由外界传入mItemLayout，通过inflater来获取
     *
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup,  int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        BaseViewHolder vh = new BaseViewHolder(view,onRecyclerViewListener,datas);
        return vh;
    }

    /**
     * 绑定数据，提供抽象方法，由子类传入(数据，视图)，
     *
     * @param viewHoder
     * @param position
     */
    @Override
    public void onBindViewHolder(BaseViewHolder viewHoder,  int position) {
        T item;
        if (position >=datas.size()) {
            item = null;
        } else {
            item = datas.get(position);
        }
        convert((H)viewHoder, item,position);
        /*//添加动画效果
        setItemAppearAnimation(viewHoder, position, R.anim.anim_bottom_in);*/
    }
    //设置动画效果
    protected int mLastPosition = -1;
    protected void setItemAppearAnimation(RecyclerView.ViewHolder holder, int position, @AnimRes int type) {
        if (position > mLastPosition/* && !isFooterPosition(position)*/) {
            Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(), type);
            holder.itemView.startAnimation(animation);
            mLastPosition = position;
        }
    }


    @Override
    public int getItemCount() {
        if(datas==null || datas.size()<=0)
            return 0;

        return datas.size();
    }


    public T getItem(int position) {
        if (position >= datas.size()) return null;
        return datas.get(position);
    }


    public void clear(){
//        int itemCount = datas.size();
//        datas.clear();
//        this.notifyItemRangeRemoved(0,itemCount);

        if(datas==null || datas.size()<=0)
            return;

        for (Iterator it=datas.iterator();it.hasNext();){

            T t = (T) it.next();
            int position = datas.indexOf(t);
            it.remove();
            notifyItemRemoved(position);
        }
    }

    /**
     * 从列表中删除某项
     * @param t
     */
    public  void removeItem(T t){

        int position = datas.indexOf(t);
        datas.remove(position);
        notifyItemRemoved(position);
    }


    /**
     * 添加数据
     *
     * @param list
     */
    public void addList(List<T> list) {
        this.datas.addAll(list);
    }

    public void clearList() {
        this.datas.clear();
    }


    public List<T> getDatas(){

        return  datas;
    }




    public void addData(List<T> datas){

        addData(0,datas);
    }

    public void addData(int position,List<T> list){

        if(list !=null && list.size()>0) {

            for (T t:list) {
                datas.add(position, t);
                notifyItemInserted(position);
            }

        }
    }



    public void refreshData(List<T> list){

        clear();
        if(list !=null && list.size()>0){


            int size = list.size();
            for (int i=0;i<size;i++){
                datas.add(i,list.get(i));
                notifyItemInserted(i);
            }

        }
    }

    public void loadMoreData(List<T> list){

        if(list !=null && list.size()>0){

            int size = list.size();
            int begin = datas.size();
            for (int i=0;i<size;i++){
                datas.add(list.get(i));
                notifyItemInserted(i+begin);
            }

        }

    }



    /**
     * Implement this method and use the helper to adapt the view to the given item.
     * @param viewHoder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    protected abstract void convert(H viewHoder, T item,int position);








}
